package com.linsen.core.web.aspect;

import com.linsen.core.common.aspect.BaseAspect;
import com.linsen.core.common.constant.LinsenConstant;
import com.linsen.core.common.exception.RequestBusinessException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhaoyong
 * @date 2023/3/28
 */
@Aspect
@Slf4j
@Order(Ordered.HIGHEST_PRECEDENCE / 2 - 1)
@RequiredArgsConstructor
public class RequestLocalAspect extends BaseAspect {
    private final static List<String> LOCAL_IP_POOL = Arrays.asList("localhost", "127.0.0.1", "0:0:0:0:0:0:0:1", "172.17.0.1");

    @Pointcut("@annotation(" + LinsenConstant.LINSEN_APP_PACKAGE + ".core.web.annotation.Local)")
    public void pointcut() {
    }

    @Before("pointcut()")
    public void before(JoinPoint point) {
        String ip = super.getClientIP();
        if (!LOCAL_IP_POOL.contains(ip)) {
            throw new RequestBusinessException("api request is not access");
        }
    }
}